package com.blog.cmrpersonalblog.dto.category.request;

import lombok.Data;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 分类批量操作请求
 */
@Data
public class CategoryBatchOperationRequest {

    /**
     * 分类ID列表
     */
    @NotEmpty(message = "分类ID列表不能为空")
    private List<Long> categoryIds;

    /**
     * 操作类型
     */
    @NotBlank(message = "操作类型不能为空")
    private String operation;

    /**
     * 目标状态（用于批量启用/禁用）
     */
    private Integer targetStatus;

    /**
     * 操作原因
     */
    private String reason;

    /**
     * 批量操作类型枚举
     */
    public enum OperationType {
        ENABLE("ENABLE", "启用"),
        DISABLE("DISABLE", "禁用"),
        DELETE("DELETE", "删除"),
        UPDATE_SORT("UPDATE_SORT", "更新排序");

        private final String code;
        private final String description;

        OperationType(String code, String description) {
            this.code = code;
            this.description = description;
        }

        public String getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static OperationType fromCode(String code) {
            for (OperationType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return null;
        }
    }

    /**
     * 获取操作类型枚举
     */
    public OperationType getOperationTypeEnum() {
        return OperationType.fromCode(operation);
    }

    /**
     * 验证操作类型是否有效
     */
    public boolean isValidOperationType() {
        return getOperationTypeEnum() != null;
    }

    /**
     * 是否为启用操作
     */
    public boolean isEnable() {
        return OperationType.ENABLE.equals(getOperationTypeEnum());
    }

    /**
     * 是否为禁用操作
     */
    public boolean isDisable() {
        return OperationType.DISABLE.equals(getOperationTypeEnum());
    }

    /**
     * 是否为删除操作
     */
    public boolean isDelete() {
        return OperationType.DELETE.equals(getOperationTypeEnum());
    }

    /**
     * 是否为更新排序操作
     */
    public boolean isUpdateSort() {
        return OperationType.UPDATE_SORT.equals(getOperationTypeEnum());
    }

    /**
     * 验证请求数据是否有效
     */
    public boolean isValid() {
        return categoryIds != null && !categoryIds.isEmpty() &&
               isValidOperationType() &&
               categoryIds.stream().allMatch(id -> id != null && id > 0);
    }
}
